// pages/student/studentmanage/studentmanage.js
import sendRequest from '../../rpromise/request'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        current: 0,
        tabs: ['已报名', '已参与'],
        index: null,
        sno:'',
        activity:[],
        getchecksign: ['签到', '签退'],
    },
    tabSelect(e) {
        // console.log(e.currentTarget.dataset.id)
        this.setData({
            current: e.currentTarget.dataset.id
        })
    },
    async checkingBtn(e){
        console.log(e)
        var ano = this.data.activity[e.currentTarget.dataset.id].ano
        var sql = "SELECT signin,signout FROM activity WHERE ano="+ano
        var sign = {}
        var type = this.data.getchecksign[e.detail.value]
        console.log(sql)
        var req = await this.sqlRequest(sql).then(res=>{
            console.log(res.data[0])
            sign = res.data[0]
        })
        if(sign.signin==='Y'&&type==='签到'){
            wx.navigateTo({
                url: '/pages/checking/checking?ano='+ano+"&sign=签到",
              })
        }else if(sign.signout==='Y'&&type==='签退'){
            wx.navigateTo({
                url: '/pages/checking/checking?ano='+ano+"&sign=签退",
              })
        }else{
            wx.showToast({
              title: '负责人未开启'+type,
              icon:'none',
            })
        }
        
    },
    LookActivity(e){
        var i = e.currentTarget.dataset.id
        var adata = JSON.stringify(this.data.activity[i])
        console.log(adata)
        wx.navigateTo({
            url: '/pages/student/studentactivity/studentactivity?adata=' + adata,
        })
    },
    loadData(){
        var that = this
        var sql = "SELECT * FROM activity WHERE ano in(SELECT ano FROM sa WHERE sno='"+this.data.sno+"')"
        console.log(sql)
        this.sqlRequest(sql).then(res=>{
            console.log(res.data)
            that.setData({
                activity:res.data,
            })
        })
    },
    async sqlRequest(sql) {
        const res1 = await sendRequest({
            method: 'POST',
            url: '/manage',
            data: {
                sql: sql
            },
        })
        // console.log(res1)
        return res1
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        wx.setNavigationBarTitle({
          title: options.title,
        })
        this.setData({
            sno:wx.getStorageSync('student').user
        })
        this.loadData()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})